<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="编号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="success" @click="onPayment('robot-num')">
          <Icon icon="ep:goods-filled" class="mr-5px" /> 购买坐席
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" border>
      <el-table-column label="编号" align="center" prop="no" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <div>
            <el-tag v-if="scope.row.status == 1" type="success">已启用</el-tag>
            <el-tag v-if="scope.row.status == 0" type="info">未启用</el-tag>
            <el-tag v-if="scope.row.status == 9" type="danger">已过期</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="有效期（天）" align="center" prop="expireTime" />
      <el-table-column label="失效日期" align="center" prop="expireAt" :formatter="dateFormatter" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
      />
      <el-table-column align="center" label="操作" width="100">
        <template #default="scope">
          <el-button
            type="success"
            :disabled="scope.row.expireTime == 0"
            link
            @click="onRenew('robot-num', scope.row.id, scope.row.num)"
            >续费</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <SkuFormDialog ref="skuFormDialog" />

  <RenewPackageDialog ref="renewPackgeRef" />
</template>

<script setup lang="ts">
import RenewPackageDialog from '@/components/RenewPackageDialog/index.vue'
import SkuFormDialog from '@/components/SkuFormDialog/index.vue'
import { dateFormatter } from '@/utils/formatTime'

import * as AgentApi from '@/api/robot/agent'
defineOptions({ name: 'RobotAgent' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: null,
  status: null,
  taskId: null,
  expireTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AgentApi.getAgentPage(queryParams)
    list.value = data.list
    // list.value.push({
    //   "id": 66,
    //   "no": "202405081444055",
    //   "status": 9,
    //   "taskId": null,
    //   "expireTime": 30,
    //   "expireAt": 1717742646000,
    //   "createTime": 1715150646000,
    //   "tenantId": 1
    // })
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const skuFormDialog = ref()
// 吊起支付方法
const onPayment = (code: string, type?: number = 2) => {
  skuFormDialog.value.open(type, code, null)
}

const renewPackgeRef = ref()
// 吊起续费方法
const onRenew = (code: string, rowId: number, num: number) => {
  renewPackgeRef.value.open(code, rowId, num)
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
